﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using cndp.Model;

namespace cndp.View
{
    public partial class FormEditEvent : Form
    {
        public BindingList<Event> EventList = new BindingList<Event>();
        public string OldValue;

        public FormEditEvent()
        {
            InitializeComponent();
        }

        private void FormEditEvent_Load(object sender, EventArgs e)
        {
            KeyPreview = true;
        }

        private void FormEditEvent_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Escape)
                Close();
        }

        private void dataGridViewEventInformation_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            OldValue = dataGridViewEventInformation[e.ColumnIndex, e.RowIndex].Value.ToString();
        }

        private void dataGridViewEventInformation_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            var d = new Datacheck();
            var row = dataGridViewEventInformation[e.ColumnIndex, e.RowIndex];
            if (row.ColumnIndex == 0)
            {
                if (!d.CheckIfInteger(row.Value.ToString()))
                {
                    MessageBox.Show(@"Fel format.", @"Ogiltigt data.");
                    row.Value = OldValue;
                }
                var idNotTaken = true;
                for (var i = 0; i < EventList.Count; ++i)
                {
                    if (((int) row.Value == EventList[i].Id) && (i != row.RowIndex))
                    {
                        idNotTaken = false;
                    }
                }
                if (!idNotTaken)
                {
                    MessageBox.Show(@"Gren-ID redan taget", @"Ogiltigt data");
                    row.Value = OldValue;
                }
            }
            if (row.ColumnIndex == 2)
            {
                if (!(row.Value.ToString() == "Herr" || row.Value.ToString() == "Dam"))
                {
                    MessageBox.Show(@"Kön måste vara Herr eller Dam", @"Ogiltigt data");
                    row.Value = OldValue;
                }
            }
        }
    }
}